<?php
   /*************************************************************************
    * File    : AcceptPayment.php
    * Version : $Id: AcceptPayment.php,v 1.14 2009/04/02 05:49:47 thomas Exp $
    * Author  : wolfgang.schaefer@mPAY24.com
    *************************************************************************/

class AcceptPayment {

   // set-methods
   function setMerchantId($id) {
      if(is_numeric($id)) {
         $this->merchantid = urlencode($id);
      }
   }

   function setPrice($price) {
      if(is_numeric($price)) {
         $this->price = urlencode($price);
      }
   }

   function setCurrency($currency) {
      switch($currency) {
         case 'EUR' :
            $this->currency = urlencode($currency);
            break;
         case 'CHF' :
            $this->currency = urlencode($currency);
            break;
         case 'USD' :
            $this->currency = urlencode($currency);
            break;
         case 'GBP' :
            $this->currency = urlencode($currency);
            break;
         // ...
      }
   }

   function setIdentifier($identifier) {
      switch($this->p_type) {
         case 'CC' :
            if(is_numeric($identifier) && strlen($identifier) >= 12 && strlen($identifier) <= 16) {
               $this->identifier = urlencode($identifier);
            }
         break;
         case 'MAESTRO' :
            if(is_numeric($identifier) && strlen($identifier) <= 18) {
               $this->identifier = urlencode($identifier);
            }
         break;
         case 'PB' :
            if(is_numeric($identifier)) {
               $this->identifier = urlencode($identifier);
            }
         break;
         case 'MIA' :
            if(is_numeric($identifier)) {
               $this->identifier = urlencode($identifier);
            }
         break;
    case 'ELV' :
            if(is_numeric($identifier)) {
               $this->identifier = urlencode($identifier);
            }
         break;
      }
   }

   function setExpiry($expiry) {
      if(is_numeric($expiry) && strlen($expiry) == 4) {
         $this->expiry = urlencode($expiry);
      }
   }

   function setTid($tid) {
      $this->tid = urlencode($tid);
   }

   function setPType($type) {
      $type = strtoupper($type);
      $this->p_type = urlencode($type);
   }

   function setOrderDesc($desc) {
      $this->orderdesc = urlencode($desc);
   }

   function setBrand($brand) {
      $brand = strtoupper($brand);
      if($this->p_type == "CC") {
         switch($brand) {
            case 'AMEX' :
               $this->brand = urlencode($brand);
            break;
            case 'DINERS' :
               $this->brand = urlencode($brand);
            break;
            case 'JCB' :
               $this->brand = urlencode($brand);
            break;
            case 'MASTERCARD' :
               $this->brand = urlencode($brand);
            break;
            case 'VISA' :
               $this->brand = urlencode($brand);
            break;
         }
        }
   elseif($this->p_type == "ELV") {
       switch($brand) {
         case 'ATOS' :
            $this->brand = urlencode($brand);
         break;
         case 'HOBEX-AT' :
            $this->brand = urlencode($brand);
         break;
         case 'HOBEX-DE' :
            $this->brand = urlencode($brand);
         break;
         case 'HOBEX-NL' :
            $this->brand = urlencode($brand);
         break;
       }
      } elseif($this->p_type == "EPS") {
         switch($brand) {
            case 'BA' :
               $this->brand = urlencode($brand);
            break;
            case 'BAWAG' :
               $this->brand = urlencode($brand);
            break;
            case 'ERSTE' :
               $this->brand = urlencode($brand);
            break;
            case 'HYPO' :
               $this->brand = urlencode($brand);
            break;
            case 'RZB' :
               $this->brand = urlencode($brand);
            break;
            case '101' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '093' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '029' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '019' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '011' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '002' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '015' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '109' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);
            break;
            case '107' :
               $this->brand = urlencode("ARZ");
               $this->bankid = urlencode($brand);

         }
      } elseif($this->p_type == "MIA") {
         switch($brand){
            case 'ONE' :
               $this->brand = urlencode($brand);
               break;
            case 'ORANGE' :
               $this->brand = urlencode($brand);
               break;
            case 'T-MOBILE' :
               $this->brand = urlencode($brand);
               break;
         }
      } elseif($this->p_type == "PSC") {
         switch($brand){
            case 'PSC':
               $this->brand = urlencode($brand);
               break;
            case 'CASH-TICKET':
               $this->brand = urlencode($brand);
               break;
            default:
               $brand = "PSC";
               $this->brand = urlencode($brand);
               break;
         }
      }
   }

   function setClearing($clearing) {
      if($clearing == 0 || $clearing == 1) {
         $this->clearing = urlencode($clearing);
      }
   }

   function setCustomer($customer) {
      $this->customer = urlencode($customer);
   }
   
   function setCustomerEmail($email) {
      $this->customer_email = urlencode($email);
   }

   function setLanguage($language) {
      $language = strtoupper($language);
      switch($language) {
         case 'EN' :
            $this->language = urlencode($language);
            break;
         case 'DE' :
            $this->language = urlencode($language);
            break;
         case 'FR' :
            $this->language = urlencode($language);
            break;
         case 'BG' :
            $this->language = urlencode($language);
            break;
         default   :
            $this->language = urlencode("DE");
            break;
      }
   }

   function setCvc($cvc) {
      if(is_numeric($cvc) && strlen($cvc) <= 4) {
         $this->cvc = urlencode($cvc);
      }
   }

   function setUserField($userfield) {
      $this->userfield = urlencode($userfield);
   }

   function setBillingAddr($addr) {
      $this->billing_addr = urlencode($addr);
   }

   function setSortCode($sortcode) {
     $this->sort_code = urlencode($sortcode);
   }
   
   function setThreeDs($threeds) {
      if ($threeds) $this->threeds="Y";
     else $this->threeds="N";
   }

   function setSuccessUrl($url) {
      $this->success_url = urlencode($url);
   }

   function setErrorUrl($url) {
      $this->error_url = urlencode($url);
   }
   
   function setConfirmUrl($url){
      $this->confirm_url = urlencode($url);
   }

   // get-methods
   function getResponse() {
      return $this->response;
   }

   // private 
   function determineOperation() {
   $this->operation = urlencode("ACCEPTPAYMENT");
   }

   // private
   function isValid() {
      $status = true;
      // check for mandatory parameter
      if($this->merchantid == "") {
         print("Error : No/Wrong MERCHANTID specified !<br>\n");
         $status = false;
      } 
      if($this->p_type == "") {
         print("Error : No/Wrong P_TYPE specified !<br>\n");
         $status = false;
      } 
      if($this->price == "") {
         print("Error : No/Wrong PRICE specified !<br>\n");
         $status = false;
      } 
      if($this->currency == "") {
         print("Error : No/Wrong CURRENCY specified !<br>\n");
         $status = false;
      } 

      switch($this->p_type) {
         case 'CC' :
            $status = $this->checkCCParams();
         break;
         case 'MAESTRO' :
            $status = $this->checkMaestroParams();
         break;
         case 'PB' :
            $status = $this->checkPBParams();
         break;
         case 'PSC' :
            $status = $this->checkPSCParams();
         break;
         case 'EPS' :
            $status = $this->checkEPSParams();
         break;
         case 'QUICK' :
            $status = $this->checkQuickParams();
         break;
         case 'MIA' :
            $status = $this->checkMIAParams();
         break;
         case 'GIROPAY' :
            $status = $this->checkGIROPAYParams();
      }

      return $status;
   }

   // private
   function checkCCParams() {
      $status = true;

      if($this->identifier == "") {
         print("Error : No/Wrong IDENTIFIER specified !<br>\n");
         $status = false;
      } 
      if($this->brand == "") {
         print("Error : No/Wrong BRAND specified !<br>\n");
         $status = false;
      } 
      if($this->expiry == "") {
         print("Error : No/Wrong EXPIRY specified !<br>\n");
         $status = false;
      } 
      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 
      return $status;
   }

   // private
   function checkMaestroParams() {
      $status = true;

      if($this->identifier == "") {
         print("Error : No/Wrong IDENTIFIER specified !<br>\n");
         $status = false;
      } 
      if($this->expiry == "") {
         print("Error : No/Wrong EXPIRY specified !<br>\n");
         $status = false;
      } 
      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 
      return $status;
   }
   
   //private
  function checkELVParams() {
     $status = true;

      if($this->identifier == "") {
         print("Error : No/Wrong IDENTIFIER specified !<br>\n");
         $status = false;
      }
      if($this->sort_code == "") {
         print("Error : No/Wrong SORT_CODE specified !<br>\n");
         $status = false;
      }
  }

   // private
   function checkPBParams() {
      $status = true;

      if($this->identifier == "") {
         print("Error : No/Wrong IDENTIFIER specified !<br>\n");
         $status = false;
      } 
      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 

      return $status;
   }

   // private
   function checkPSCParams() {
      $status = true;

      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 

      return $status;
   }

   // private
   function checkEPSParams() {
      $status = true;

      if($this->brand == "") {
         print("Error : No/Wrong BRAND specified !<br>\n");
         $status = false;
      } 
      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 

      return $status;
   }

   // private
   function checkQuickParams() {
      $status = true;

      return $status;
   }

   // private
   function checkMIAParams() {
      $status = true;

      if($this->brand == "") {
         print("Error : No/Wrong BRAND specified !<br>\n");
         $status = false;
      } 
      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 
      if($this->category_barring == "") {
         print("Error : No/Wrong CategoryBarring specified !<br>\n");
         $status = false;
      } 

      return $status;
   }

   function checkGIROPAYParams() {
      $status = true;
      
      if($this->tid == "") {
         print("Error : No/Wrong TID specified !<br>\n");
         $status = false;
      } 
      if($this->sort_code == "") {
         print("Error : No/Wrong SORT_CODE specified !<br>\n");
         $status = false;
      }
      
      return $status;
   }

   // private
   function buildRequest() {
      $this->request = "OPERATION=" . $this->operation      . "&" .
         "MERCHANTID="           . $this->merchantid     . "&" .
         "TID="                  . $this->tid         . "&" .
         "P_TYPE="               . $this->p_type         . "&" .
         "BRAND="                . $this->brand       . "&" .
         "BANK_ID="              . $this->bankid         . "&" .
         "IDENTIFIER="           . $this->identifier     . "&" .
         "CVC="                  . $this->cvc         . "&" .
         "EXPIRY="               . $this->expiry         . "&" .
         "SORT_CODE="            . $this->sort_code      . "&" .
         "CURRENCY="             . $this->currency    . "&" .
         "PRICE="                . $this->price       . "&" .
         "AUTH_3DS="             . $this->threeds     . "&" .
         "SUCCESS_URL="          . $this->success_url . "&" .
         "ERROR_URL="            . $this->error_url      . "&" .
         "CONFIRMATION_URL="        . $this->confirm_url . "&" .
         "USER_FIELD="           . $this->userfield      . "&" .
         "ORDERDESC="            . $this->orderdesc      . "&" .
         "CUSTOMER="             . $this->customer    . "&" .
         "LANGUAGE="             . $this->language    . "&" .
         "CUSTOMER_EMAIL="       . $this->customer_email;

   }

   // public
   function send() {
      if($this->isValid()) {
         // determine operation
         $this->determineOperation();
         
         // build Request
         $this->buildRequest();

         // send request;
         $ch = curl_init($this->etp_url);
   
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         //curl_setopt($ch, CURLOPT_CAINFO, "./curl-ca-bundle.crt");
      
         // get response
         $this->response = curl_exec($ch);

         curl_close($ch);
      }
   }

   function getLocation() {
      return $this->location;
   }

   // public
   function parseResponse() {
      $parameters = split("&", $this->response);

      for($i=0; $i<count($parameters);$i++) {
         list($html_resp[$i]['name'],$html_resp[$i]['value'])     = split("=",$parameters[$i]);
         if(strtoupper($html_resp[$i]['name']) == 'STATUS')       $this->status     = strtoupper($html_resp[$i]['value']);
         if(strtoupper($html_resp[$i]['name']) == 'RETURNCODE')   $this->returncode = strtoupper($html_resp[$i]['value']);
         if(strtoupper($html_resp[$i]['name']) == 'LOCATION')     $this->location   = urldecode($html_resp[$i]['value']);
         if(strtoupper($html_resp[$i]['name']) == 'MPAYTID')      $this->mpaytid    = $html_resp[$i]['value'];
      }
      
      if(strrpos(urldecode($this->success_url),"?") || strrpos(urldecode($this->error_url),"?"))
               $sep = "&";
            else
               $sep = "?";
      
      if ($this->status == "OK") {
         if ($this->returncode == "REDIRECT")
            header("Location:" . $this->location);
         else{
            header("Location:" . urldecode($this->success_url) . $sep . "RETURNCODE=" .
               $this->returncode . "&STATUS=" . $this->status .
               "&MPAYTID=" . $this->mpaytid);
         }
      } else {
         header("Location:" . urldecode($this->error_url) . $sep . "RETURNCODE=" .
            $this->returncode . "&STATUS=" . $this->status . "&MPAYTID=" . $this->mpaytid);
      }
   }
   
   // Constructor
   function AcceptPayment($ptype, $etp_url) {
      $this->setPType($ptype);

      if($etp_url != "") {
         $this->etp_url = $etp_url;
      } else {
         echo "No ETP-URL specified !";
         exit(1);
      }
   }

   //
   // -------- member variables --------
   //
   // request
   var $request;
   var $operation;
   var $merchantid;
   var $timestamp;
   var $price;
   var $currency;
   var $identifier;
   var $expiry;
   var $tid;
   var $p_type;
   var $orderdesc;
   var $brand;
   var $clearing;
   var $customer;
   var $customer_email;
   var $language;
   var $cvc;
   var $userfield;
   var $billing_addr;
   var $threeds;
   var $sortcode;
   var $bankid;

   // etp
   var $etp_url;
   
   // success- and error-urls
   var $success_url;
   var $error_url;
   var $confirm_url;


   // response
   var $response;
   var $status;
   var $returncode;
   var $location;
   var $errno;
   var $externalstatus;
   var $mpaytid;
}

?>